//
//  CMJNavgationController.m
//  CMJWeibo
//
//  Created by BH on 15/12/3.
//
//

#import "CMJNavgationController.h"
#import "CMJCommon.h"
#import "UIBarButtonItem+MJ.h"
#import "UIImage+MJ.h"

@implementation CMJNavgationController

+(void)initialize
{
    //设置导航栏主题
    [self setUpNavTheme];
    
    //设置导航栏按钮主题
    [self setUpNavItemTheme];
    
}

/**
 * 设置导航栏主题
 */
+ (void)setUpNavTheme
{
    //获取appearence对象，修改导航栏主题
    UINavigationBar *bar = [UINavigationBar appearance];
    
    //设置背景
    if (!iOS7) {
        [bar setBackgroundImage:[UIImage imageWithName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
        
        //设置文字
        NSMutableDictionary *textDic = [NSMutableDictionary dictionary];
        textDic[NSForegroundColorAttributeName] = [UIColor blackColor];
        textDic[NSShadowAttributeName] = [NSValue valueWithUIOffset:UIOffsetZero];
        textDic[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20.f];
        [bar setTitleTextAttributes:textDic];
    }
}

/**
 * 设置导航栏按钮主题
 */
+ (void)setUpNavItemTheme
{
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 2.设置背景
    /******普通状态******/
    // 按钮文字
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    // 设置文字颜色
    textAttrs[UITextAttributeTextColor] = [UIColor orangeColor];
    // 去掉阴影
    textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero];
    // 设置文字字体
    textAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:16];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    /******高亮状态******/
    NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionary];
    highTextAttrs.dictionary = textAttrs;
    // 设置文字颜色
    highTextAttrs[UITextAttributeTextColor] = [UIColor redColor];
    [item setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
    
    /******不可用状态******/
    NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
    disableTextAttrs.dictionary = textAttrs;
    // 设置文字颜色
    disableTextAttrs[UITextAttributeTextColor] = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.3];
    [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
    
    //设置背景
    if (!iOS7) {
        [item setBackButtonBackgroundImage:[UIImage imageNamed:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    }
    
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    if (iOS7) {
        // 清空手势识别器的代理, 就能恢复以前滑动移除控制器的功能
        self.interactivePopGestureRecognizer.delegate = nil;
    }
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        // 如果push的不是根控制器，隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        
        //导航栏左上角返回按钮
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" highImage:@"navigationbar_back_highlighted" target:self action:@selector(back)];
        
        //导航栏右上角更多按钮
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" highImage:@"navigationbar_more_highlighted" target:self action:@selector(more)];
    }
    [super pushViewController:viewController animated:animated];
}

-(void)back
{
    [self popViewControllerAnimated:YES];
}

-(void)more
{
    [self popToRootViewControllerAnimated:YES];
}

@end
